local sk__zhiming = fk.CreateSkill {

  name = "sk__zhiming",

  tags = {},

}



sk__zhiming:addEffect("active", {
  name = "sk__zhiming",
  anim_type = "control",
  card_num = 0,
  min_target_num = 1,
  prompt = function(self, player)
    return "#sk__zhiming:::" .. player.hp
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(sk__zhiming.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected < player.hp and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:sortByAction(effect.tos)
    local targets = effect.tos
    local id = 1
    local current_phase = room.logic:getCurrentEvent():findParent(GameEvent.Phase, true)
    room.logic:getEventsByRule(GameEvent.Phase, 1, function(e)
      if e.data.who == player and e.data.phase == Player.Play and current_phase and e.id ~= current_phase.id then
        id = e.id
        return true
      end
    end, 1)
    local mark = player:getTableMark(sk__zhiming.name)
    if #mark > 0 and mark[1] ~= id then
      mark = {}
    end
    local choices = {}
    if #mark == 0 or not table.contains(mark[2], 1) then
      if room:askForSkillInvoke(player, sk__zhiming.name, nil, "#sk__zhiming1-invoke") then
        table.insert(choices, 1)
        room:loseHp(player, 1, sk__zhiming.name)
      end
    end
    if not player.dead and (#mark == 0 or not table.contains(mark[2], 2)) then
      if room:askForSkillInvoke(player, sk__zhiming.name, nil, "#sk__zhiming2-invoke") then
        table.insert(choices, 2)
        player:turnOver()
      end
    end
    local n = 0
    if not player.dead and not player:isNude() and (#mark == 0 or not table.contains(mark[2], 3)) then
      local cards = room:askForDiscard(player, 1, 999, true, sk__zhiming.name, true, ".", "#sk__zhiming3-invoke")
      if #cards > 0 then
        table.insert(choices, 3)
        n = #cards
      end
    end
    for _, p in ipairs(targets) do
      if not p.dead and table.contains(choices, 1) then
        room:loseHp(p, 1, sk__zhiming.name)
      end
      if not p.dead and table.contains(choices, 2) then
        p:turnOver()
      end
      if not p.dead and not p:isNude() and table.contains(choices, 3) then
        room:askForDiscard(p, n, n, true, sk__zhiming.name, false)
      end
    end
    if not player.dead and player.phase == Player.Play and current_phase then
      room:setPlayerMark(player, sk__zhiming.name, { current_phase.id, choices })
      return
    end
    room:setPlayerMark(player, sk__zhiming.name, 0)
  end,
})

return sk__zhiming
